﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Chen.Common;
using Chen.DB;
using DBDcoumentCreater.Lib;
using Chen.Ext;
using System.Threading;
namespace DBDcoumentCreater
{
    public partial class Form1 : Form
    {
        private string defaultHtml = "数据库表目录.html";
        private IniFileHelp ini = new IniFileHelp(".//set.ini");
        private IDAL dal;

        public Form1()
        {
            InitializeComponent();

            txtTitle.Text = ini.GetString("Set", "title", "数据库帮助文档");
            cbbDbtype.SelectedIndex = ini.GetInt32("Set", "index", 0);
            if (cbbDbtype.SelectedIndex == 0)
            {
                txtConn.Text = ini.GetString("Set", "index_0", "");
            }
            else if (cbbDbtype.SelectedIndex == 1)
            {
                txtConn.Text = ini.GetString("Set", "index_1", "");
            }
            else if (cbbDbtype.SelectedIndex == 2)
            {
                txtConn.Text = ini.GetString("Set", "index_2", "");
            }
        }

        private void btnConn_Click(object sender, EventArgs e)
        {
            if (txtConn.Text.Length == 0)
            {
                lblMessage.Text = "连接字符串不能为空";
                return;
            }
            try
            {
                dal = DALFacotry.Create(cbbDbtype.Text, txtConn.Text);
                if (dal == null)
                {
                    lblMessage.Text = "暂时不支持该数据库 敬请期待";
                    return;
                }
            }
            catch (Exception)
            {
                lblMessage.Text = "数据库异常 请确认";
                return;
            }

            lblMessage.Text = "成功连接数据库";
            //成功连接之后将配置保存
            ini.WriteValue("Set", "index", cbbDbtype.SelectedIndex);
            ini.WriteValue("Set", "index_" + cbbDbtype.SelectedIndex, txtConn.Text);
            //加载表信息
            ckbTables.Items.Clear();
            ckbData.Items.Clear();
            var dt = dal.GetTables();
            if (dt.Rows.Count == 0)
            {
                lblMessage.Text = "查询表信息异常，请选择正确的数据库!";
                return;
            }
            foreach (DataRow dr in dt.Rows)
            {
                ckbTables.Items.Add(dr["表名"].ToString());
                ckbData.Items.Add(dr["表名"].ToString());
            }
            groupBox1.Enabled = true;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (ckbData.CheckedItems.Count == 0 && ckbTables.CheckedItems.Count == 0)
            {
                lblMessage.Text = "请至少选择一张表";
                return;
            }
            if (txtTitle.Text.Length == 0)
            {
                lblMessage.Text = "请输入CHM文件标题";
                return;
            }
            Thread thread = new Thread(CrateCHM);
            thread.IsBackground = true;
            thread.Start();
        }

        private void CrateCHM()
        {
            lblMessage.Text = "正在导出...";
            //保存配置
            ini.WriteValue("Set", "title", txtTitle.Text);
            Directory.CreateDirectory(".//tmp");
            var dtAll = dal.GetTables().Copy();
            //将表名修改为链接
            for (var i = 0; i < dtAll.Rows.Count; i++)
            {
                //这里的文件名规则一定要去保存html的文件名规则一致 否则链接会找不到文件的
                var ret = "<a href=\"表结构\\{0}  {1}.html\">{0}</a>".FormatString(dtAll.Rows[i]["表名"].ToString(), dtAll.Rows[i]["表说明"].ToString());
                dtAll.Rows[i]["表名"] = ret;
            }
            dtAll.TableName = "<b>数据库表目录</b>";
            DbCommon.CreateHtml(dtAll, false, "./tmp/" + defaultHtml, false);
            #region 导出表结构
            //传递导出的表结构 table选中项 得到表结构详情
            List<string> lst = new List<string>();
            foreach (var item in ckbTables.CheckedItems)
            {
                lst.Add(item.ToString());
            }
            if (lst.Count > 0)
            {
                var lstDt = dal.GetTableStruct(lst);
                //创建表结构的html
                var pathTables = "./tmp/表结构";
                Directory.CreateDirectory(pathTables);
                foreach (var dt in lstDt)
                {
                    //得到表描述
                    var drs = dal.GetTables().Select("表名='" + dt.TableName + "'");
                    var desp = string.Empty;
                    if (drs.Length > 0) desp = drs[0]["表说明"].ToString();
                    DbCommon.CreateHtml(dt, true, Path.Combine(pathTables, dt.TableName + "  " + desp + ".html"), true, desp);
                }
            }
            #endregion

            #region 导出表数据
            //传递需要导出数据的table选中项  得到数据内容
            lst.Clear();
            foreach (var item in ckbData.CheckedItems)
            {
                lst.Add(item.ToString());
            }
            if (lst.Count > 0)
            {
                var lstDt = dal.GetTableData(lst);
                //创建常用数据的html
                var pathTables = "./tmp/常用数据";
                Directory.CreateDirectory(pathTables);
                foreach (var dt in lstDt)
                {
                    DbCommon.CreateHtml2(dt, true, Path.Combine(pathTables, dt.TableName + ".html"));
                }
            }
            #endregion

            //编译CHM文档
            ChmHelp c3 = new ChmHelp();
            c3.DefaultPage = defaultHtml;
            c3.Title = txtTitle.Text;
            c3.ChmFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), c3.Title + ".chm");
            c3.SourcePath = @"./tmp";
            c3.Compile();
            lblMessage.Text = "导出完毕 文件存储在:" + c3.ChmFileName;
            //Directory.Delete("./tmp", true);
        }


        private void txtConn_TextChanged(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
        }

        #region 全选控制
        private void btnChoseAll2_Click(object sender, EventArgs e)
        {
            ckbData.SelectedAll();
        }

        private void btnChoseReverse2_Click(object sender, EventArgs e)
        {
            ckbData.SelectedReverse();
        }

        private void btnChoseAll_Click(object sender, EventArgs e)
        {
            ckbTables.SelectedAll();
        }

        private void btnChoseReverse_Click(object sender, EventArgs e)
        {
            ckbTables.SelectedReverse();
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            cbbDbtype.Items.Remove("SQL2012");
            cbbDbtype.Items.Remove("Oracle");
        }
    }
}
